<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6
 * Time: 17:19
 */

namespace App\Game\Traits;


use App\Models\QuotaHandicap;
use App\Models\Def\MainGamePlat;

trait SunBetTrait
{
    //获取认证
    private function token()
    {
        $json = [
            'grant_type' => 'client_credentials',
            'scope' => 'playerapi',
            'client_id' => $this->clientId,
            'client_secret' => $this->clientSecret,
        ];
        $url = $this->gameUrl . self::API_TOKEN;
        $header = array_merge($this->header, ['Content-Type' => 'application/x-www-form-urlencoded']);
        $token = false;
        try{
            $json = http_build_query($json);
//            $output = $this->request('POST', $url, $json, $header);
            $output = $this->post_data($url, $json, $header);
//            file_put_contents('/home/gameapi/storage/logs/test.log', 'aaa...'.$output.PHP_EOL, FILE_APPEND);
            $output = json_decode($output, true);
            if (!empty($output['access_token'])) {
                $token = $output['access_token'];
            }
        }catch (\Exception $e){
            file_put_contents('/home/gameapi/storage/logs/test.log', 'sunbet...token_error...'.$e->getMessage().PHP_EOL, FILE_APPEND);
        }
        return $token;
    }
    //获取会员认证
    private function userToken($accountUserName, $token, $platformType = 0)
    {
        $request = request();
        $mainGamePlat = MainGamePlat::where('main_game_plat_code', $request->mainGamePlat)->first();
        $oddType = [];
        if (isset($request->odds_handicap)) {
            $oddType = QuotaHandicap::where([
                    ['code', '=', $request->odds_handicap],
                    ['plat_id', '=', $mainGamePlat->id],
                ]
            )->first();
        }
        $json = [
            'ipaddress' => $request->getClientIp(),
            'username' => $accountUserName,
            'userid' => $accountUserName,
            'lang' => 'zh-CN',
            'cur' => 'RMB',
            'betlimitid' => $oddType ?$oddType->plat_code: 1,
            'platformtype' => $platformType,//桌面版 0 手机1
            "istestplayer" => false
        ];
        $url = $this->gameUrl . self::API_USER_LOGIN;
        $header = array_merge($this->header, ['Authorization' => 'Bearer '.$token]);
        $authtoken = false;
        try{
            $json = json_encode($json);
//            $output = $this->request('POST', $url, $json, $header);
            $output = $this->post_data($url, $json, $header);
            $output = json_decode($output, true);
            if (!empty($output['authtoken'])) {
                $authtoken = $output['authtoken'];
            }
        }catch (\Exception $e){
            file_put_contents('/home/gameapi/storage/logs/test.log', 'sunbet...user_token_error...'.$e->getMessage().PHP_EOL, FILE_APPEND);
        }
        return $authtoken;
    }

    private function post_data($url,$data,$headers)
    {
        $header= [];
        foreach ($headers as $k => $v){
            $header[] = $k.':'.$v;
        }
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            file_put_contents('/home/gameapi/storage/logs/test.log', 'sunbet..post_error...'.$error.PHP_EOL, FILE_APPEND);
            return $error;
        }
        curl_close($ch);
        //var_dump($response);
        return $response;
    }
    private function get_data($url,$headers)
    {
        $header= [];
        foreach ($headers as $k => $v){
            $header[] = $k.':'.$v;
        }
        $ch = curl_init();
        if(substr($url,0,5)=='https'){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($ch);
        if($error=curl_error($ch)){
            file_put_contents('/home/gameapi/storage/logs/test.log', 'sunbet..get_error...'.$error.PHP_EOL, FILE_APPEND);
            return $error;
        }
        curl_close($ch);
        //var_dump($response);
        return $response;
  }
}